package cn.tedu.ivos.vehicle.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveDTO;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/10 9:11
 */
@Api(tags = "车辆模块")
@Validated
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    /**
     * 查询车辆
     * return:JsonResult
     */
    @GetMapping("select")
    @ApiOperation("查询车辆")
    public JsonResult select(VehicleQuery vehicleQuery) {
        List<VehicleVO> vehicleList = vehicleService.selectVehicle(vehicleQuery);
        log.info("查询到" + vehicleList.size() + "辆车");
        return JsonResult.ok(vehicleList);
    }

    /**
     * 新增车辆/修改车辆
     * return:JsonResult
     */
    @PostMapping("insert")
    @ApiOperation("新增/修改车辆")
    public JsonResult insert(VehicleSaveDTO vehicleSaveDTO) {
        log.info("新增/修改车辆");
        vehicleService.save(vehicleSaveDTO);
        return JsonResult.ok();
    }

    /**
     * 删除车辆
     * return:JsonResult
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation("删除车辆")
    public JsonResult delete(@PathVariable Long id) {
        log.info("删除车辆");
        vehicleService.deleteVehicle(id);
        return JsonResult.ok();
    }

    /**
     * 解绑车辆
     * return:JsonResult
     */
    @PostMapping("unbind/{id}")
    @ApiOperation("解绑车辆")
    public JsonResult unBind(@PathVariable Long id) {
        log.info("解绑车辆:" + id);
        vehicleService.unBind(id);
        return JsonResult.ok();
    }

    /**
     * 绑定车辆
     * return:JsonResult
     */
    @PostMapping("bind/{vehicleId}/{geoId}")
    @ApiOperation("绑定车辆")
    public JsonResult bind(@PathVariable Long vehicleId,@PathVariable Long geoId) {
        log.info("绑定车辆:" + vehicleId + "到电子围栏:" + geoId);
        vehicleService.bind(vehicleId,geoId);
        return JsonResult.ok();
    }
}
